import re


def test_patterns(text, patterns):
    for pattern, desc in patterns:
        print('{!r} ({})\n'.format(pattern, desc))
        print('  {!r}'.format(text))
        for match in re.finditer(pattern, text):
            s = match.start()
            e = match.end()
            prefix = ' ' * (s)
            print('  {}{!r}{} '.format(prefix, text[s:e], ' ' * (len(text) - e)), end=' ', )
            print(match.groups())
            if match.groupdict():
                print('{}{}'.format(' ' * (len(text) - s), match.groupdict()), )
        print()
    return


if __name__ == '__main__':
    text = "abbbaaaabbba"
    patterns = [
        (r"a(a*)(b*)", "字符 a 后面跟着 0 个及其以上的 a， 紧跟着 0 个及其以上的 b"),
        (r"a(?P<Serial_A>a*)(?P<Serival_B>b*)", "字符 a 后面跟着 0 个及其以上的 a， 紧跟着 0 个及其以上的 b"),
    ]
    test_patterns(text=text, patterns=patterns)
